//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.mdiy.action;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.mingsoft.base.entity.BaseEntity;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.annotation.LogAnn;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.constant.e.BusinessTypeEnum;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.basic.util.StringUtil;
import net.mingsoft.mdiy.biz.IPageBiz;
import net.mingsoft.mdiy.entity.PageEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

@Api(
        tags = {"后端-自定义模块接口"}
)
@Controller
@RequestMapping({"/${ms.manager.path}/mdiy/page"})
public class PageAction extends BaseAction {
    @Autowired
    private IPageBiz pageBiz;

    public PageAction() {
    }

    @ApiIgnore
    @GetMapping({"/index"})
    public String index(HttpServletResponse response, HttpServletRequest request) {
        return "/mdiy/page/index";
    }

    @ApiIgnore
    @GetMapping({"/form"})
    public String form(@ModelAttribute PageEntity page, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        if (ObjectUtil.isNotEmpty(page) && StringUtils.isNotEmpty(page.getId())) {
            BaseEntity pageEntity = (BaseEntity)this.pageBiz.getById(page.getId());
            model.addAttribute("pageEntity", pageEntity);
        }

        return "/mdiy/page/form";
    }

    @ApiOperation("查询自定义页面列表接口")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "pagePath",
            value = "自定义页面绑定模板的路径",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "pageTitle",
            value = "自定义页面标题",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "pageKey",
            value = "自定义页面访问路径",
            required = false,
            paramType = "query"
    )})
    @GetMapping({"/list"})
    @ResponseBody
    @RequiresPermissions({"mdiy:page:view"})
    public ResultData list(@ModelAttribute @ApiIgnore PageEntity page, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        BasicUtil.startPage();
        List pageList = this.pageBiz.query(page);
        return ResultData.build().success(new EUListBean(pageList, (int)BasicUtil.endPage(pageList).getTotal()));
    }

    @ApiOperation("获取自定义页面接口")
    @ApiImplicitParam(
            name = "id",
            value = "自定义页面编号",
            required = true,
            paramType = "query"
    )
    @GetMapping({"/get"})
    @RequiresPermissions({"mdiy:page:view"})
    @ResponseBody
    public ResultData get(@ModelAttribute @ApiIgnore PageEntity page, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        if (StringUtils.isEmpty(page.getId())) {
            return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("page.id")}));
        } else {
            PageEntity _page = (PageEntity)this.pageBiz.getById(page.getId());
            return ResultData.build().success(_page);
        }
    }

    @ApiOperation("保存自定义页面接口")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "pagePath",
            value = "自定义页面绑定模板的路径",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "pageTitle",
            value = "自定义页面标题",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "pageKey",
            value = "自定义页面访问路径",
            required = true,
            paramType = "query"
    )})
    @LogAnn(
            title = "保存自定义页面接口",
            businessType = BusinessTypeEnum.INSERT
    )
    @PostMapping({"/save"})
    @ResponseBody
    @RequiresPermissions({"mdiy:page:save"})
    public ResultData save(@ModelAttribute @ApiIgnore PageEntity page, HttpServletResponse response, HttpServletRequest request) {
        if (StringUtils.isBlank(page.getPagePath())) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("page.path")}));
        } else if (!StringUtil.checkLength(page.getPagePath() + "", 1, 255)) {
            return ResultData.build().error(this.getResString("err.length", new String[]{this.getResString("page.path"), "1", "255"}));
        } else if (StringUtils.isBlank(page.getPageTitle())) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("page.title")}));
        } else if (!StringUtil.checkLength(page.getPageTitle() + "", 1, 255)) {
            return ResultData.build().error(this.getResString("err.length", new String[]{this.getResString("page.title"), "1", "255"}));
        } else if (StringUtils.isBlank(page.getPageKey())) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("page.key")}));
        } else if (!StringUtil.checkLength(page.getPageKey() + "", 1, 255)) {
            return ResultData.build().error(this.getResString("err.length", new String[]{this.getResString("page.key"), "1", "255"}));
        } else {
            this.pageBiz.saveEntity(page);
            return ResultData.build().success(page);
        }
    }

    @ApiOperation("批量删除自定义页面接口")
    @LogAnn(
            title = "批量删除自定义页面接口",
            businessType = BusinessTypeEnum.DELETE
    )
    @PostMapping({"/delete"})
    @ResponseBody
    @RequiresPermissions({"mdiy:page:del"})
    public ResultData delete(@RequestBody List<PageEntity> pages, HttpServletResponse response, HttpServletRequest request) {
        int[] ids = new int[pages.size()];

        for(int i = 0; i < pages.size(); ++i) {
            ids[i] = Integer.parseInt(((PageEntity)pages.get(i)).getId());
        }

        this.pageBiz.delete(ids);
        return ResultData.build().success();
    }

    @ApiOperation("更新自定义页面接口")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "id",
            value = "自定义页面编号",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "pagePath",
            value = "自定义页面绑定模板的路径",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "pageTitle",
            value = "自定义页面标题",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "pageKey",
            value = "自定义页面访问路径",
            required = true,
            paramType = "query"
    )})
    @LogAnn(
            title = "更新自定义页面接口",
            businessType = BusinessTypeEnum.UPDATE
    )
    @PostMapping({"/update"})
    @ResponseBody
    @RequiresPermissions({"mdiy:page:update"})
    public ResultData update(@ModelAttribute @ApiIgnore PageEntity page, HttpServletResponse response, HttpServletRequest request) {
        if (StringUtils.isBlank(page.getPagePath())) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("page.path")}));
        } else if (!StringUtil.checkLength(page.getPagePath() + "", 1, 255)) {
            return ResultData.build().error(this.getResString("err.length", new String[]{this.getResString("page.path"), "1", "255"}));
        } else if (StringUtils.isBlank(page.getPageTitle())) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("page.title")}));
        } else if (!StringUtil.checkLength(page.getPageTitle() + "", 1, 255)) {
            return ResultData.build().error(this.getResString("err.length", new String[]{this.getResString("page.title"), "1", "255"}));
        } else if (StringUtils.isBlank(page.getPageKey())) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("page.key")}));
        } else if (!StringUtil.checkLength(page.getPageKey() + "", 1, 255)) {
            return ResultData.build().error(this.getResString("err.length", new String[]{this.getResString("page.key"), "1", "255"}));
        } else {
            this.pageBiz.updateById(page);
            return ResultData.build().success(page);
        }
    }

    @ApiOperation("校验参数接口")
    @GetMapping({"/verify"})
    @ResponseBody
    public ResultData verify(String fieldName, String fieldValue, String id, String idName) {
        boolean verify = false;
        if (StringUtils.isBlank(id)) {
            verify = super.validated("mdiy_page", fieldName, fieldValue);
        } else {
            verify = super.validated("mdiy_page", fieldName, fieldValue, id, idName);
        }

        return verify ? ResultData.build().success(false) : ResultData.build().success(true);
    }
}
